<template>
	<view class="car-detial">
		<y-cubar :isFixed="false" :bgColor="`rgba(255,255,255,${opacity})`" />
		<view class="swiper">
			<swiper-bar :swiperList="info.slideshow" />
		</view>
		<view class="card-info">
			<view class="title">{{ info.productName }}</view>
			<view class="desc">{{ info.brand }}{{ info.model }}</view>
			<view class="mall">
				<view class="prices">
					<text class="price">{{ info.bedrockPrice }}</text>
					<text class="price">元起/月 </text>
				</view>
				<view class="mall-right">
					<text>库存</text>
					<text>{{ info.stock }}</text>
					<text>销量</text>
					<text>{{ info.orderSum }}</text>
				</view>
			</view>
			<!-- 车辆信息 -->
			<car-info :info="info" />
			<!-- 车辆两点 -->
			<car-highlights :info="info" />
			<!-- 车辆方案 -->
			<car-progranmme :schemes="info.schemes" />
			<!-- 商家信息 -->
			<shop-info :info="info" @followChange="followChange" @backInfo="backInfo" />
			<!-- 详情介绍 -->
			<car-rich :info="info" />
			<!-- 价格明细 -->
			<!-- <car-price /> -->
		</view>
		<place-order :info="info" :demandId="demandId" :schemes="info.schemes" ref="placeRef" @success="handSuccess" />
		<!-- 底部 -->
		<action-bottom :info="info" @order="handOrder" @collChange="collChange" />
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		provide,
		watch
	} from 'vue'
	import {
		onLoad,
		onUnload,
		onPageScroll,
		onShareAppMessage,
		onShareTimeline
	} from "@dcloudio/uni-app";
	import {
		doDetail
	} from '@/apis/car.js'
	import {
		useAppsStore
	} from '@/stores/modules/app.js'
	import {
		storeToRefs
	} from 'pinia'
	import YCubar from '@/components/y-cubar/index.vue'
	import SwiperBar from './components/SwiperBar.vue'
	import CarInfo from './components/CarInfo.vue'
	import CarHighlights from './components/CarHighlights.vue'
	import CarProgranmme from './components/CarProgranmme.vue'
	import ShopInfo from './components/ShopInfo.vue'
	import CarRich from './components/CarRich.vue'
	import CarPrice from './components/CarPrice.vue'
	import ActionBottom from './components/ActionBottom.vue'
	import PlaceOrder from './components/PlaceOrder.vue'

	const {
		proxy
	} = getCurrentInstance();

	const curren = ref(0)
	const currenChange = (e) => {
		curren.value = e
	}
	provide('curren', curren)
	provide('currenChange', currenChange)

	const useStore = useAppsStore()
	const {
		getUserInfo: userInfo,
		getCityName: cityName,
		getToken: token
	} = storeToRefs(useStore)
	const {
		setPid
	} = useStore

	const opacity = ref(0)
	onPageScroll((e) => {
		const childerOpa = Number((e.scrollTop / 200).toFixed(2))
		opacity.value = childerOpa >= 1 ? 1 : childerOpa
	})

	const placeRef = ref(null)
	const handOrder = () => {
		placeRef.value.handOpen(info.value.id)
	}

	const collChange = () => {
		info.value.isCollect = !info.value.isCollect
	}
	const followChange = () => {
		info.value.isConcern = !info.value.isConcern
	}

	const backInfo = (e) => {
		info.value.isConcern = e.isConcern
	}

	const info = ref({
		strength: {},
		slideshow: [],
		schemes: [],
		detailsImg: [],
		configure: []
	})
	const init = async () => {
		const data = await doDetail(oldId)
		if (data.platform) {
			data.platform = JSON.parse(data.platform)
		} else {
			data.platform = []
		}
		data.platformName = uni.$uv.deepClone(data.platform).map(el => {
			return el.name
		}).toString()
		if (data.schemes) {
			data.schemes = JSON.parse(data.schemes)
		} else {
			data.schemes = []
		}
		if (data.strength) {
			data.strength = JSON.parse(data.strength)
		} else {
			data.strength = {}
		}
		info.value = data
	}

	const demandId = ref(0)
	let oldId = 0
	onLoad(async (e) => {
		if (e?.pid) {
			setPid(e.pid)
		}
		if (e?.demandId) {
			demandId.value = Number(e.demandId)
		}
		oldId = e.id
		init()
		if (!cityName.value) {
			await getApp().getCity()
		}
	})

	onUnload(() => {
		const eventChannel = proxy.getOpenerEventChannel()
		eventChannel.emit('backInfo', info.value);
	})

	const handSuccess = () => {
		const eventChannel = proxy.getOpenerEventChannel()
		eventChannel.emit('backInfo', info.value);
	}

	onShareAppMessage(() => {
		let path = `pagesA/car/detail?id=${info.value.id}`
		if (userInfo.value?.id) {
			path += `&pid=${userInfo.value.id}`
		}
		return {
			path
		}
	})

	onShareTimeline(() => {
		let query = `id=${info.value.id}`
		if (userInfo.value?.id) {
			path += `&pid=${userInfo.value.id}`
		}
		return {
			query
		}
	})

	watch(token, () => {
		init()
	})
</script>

<style lang="scss" scoped>
	.car-detial {
		min-height: 100vh;
		background-color: #F5F5F5;
		padding-bottom: 150rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 150rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 150rpx);

		.swiper {
			position: relative;
			z-index: 1;
		}

		.card-info {
			position: relative;
			z-index: 2;
			background-color: #F5F5F5;
			border-radius: 28rpx 28rpx 0 0;
			margin-top: -30rpx;
			box-sizing: border-box;
			padding: 32rpx;

			.title {
				font-size: 40rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.desc {
				margin-top: 8rpx;
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
			}

			.mall {
				margin-top: 24rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.prices {
					font-size: 40rpx;
					font-family: PingFang SC-Heavy, PingFang SC;
					font-weight: 800;
					color: #FF4600;

					text {
						&:last-child {
							font-size: 26rpx;
						}
					}
				}

				&-right {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;

					text {
						&:nth-child(2) {
							padding-left: 10rpx;
						}

						&:nth-child(3) {
							padding-left: 25rpx;
						}

						&:nth-child(4) {
							padding-left: 10rpx;
						}
					}
				}
			}
		}
	}
</style>